Jul 26, 2009, 05:32 AM // 05:32
|
#1
|
Guest
|
Programming question
Alright, well, I've decided to dabble into game programing. I've been programming for a few years now, and I've never really bothered with it. However, there's just one thing I'm having problems with...
The basic idea is to take a picture, and if the cursor is over the picture, allow it to be dragged, but have it drag in relation to where the mouse is.
For instance, if I dragged it from the bottom right corner, it won't snap to the top left corner.
I've gotten it down, and it works, but the code is horribly messy, and not just because of my general "I'm doing this to try it out, who cares what it looks like", and I was wondering if there was a better way of doing it.
This is the area where the dragging portion of the code is. If you're wondering, it's C# and XNA 3.1
Code:
//dummy function
protected void UpdateMouse()
{
MouseState mouse = Mouse.GetState();//Mouse functions/vars
if (!isDown && mouse.RightButton == ButtonState.Pressed)//If the right mouse button was previously not pressed and now is...
{
this.IsMouseVisible = false;//Make it invisible, so we know
if ((mouse.X > spritevect.X && mouse.X < spritevect.X + spritetext.Width) && (mouse.Y > spritevect.Y && mouse.Y < spritevect.Y + spritetext.Height))//<-- is what I'm curious about.
{//The if statement just checks to make sure that the mouse is within the box
xoffset = mouse.X - spritevect.X;//Find the offset
yoffset = mouse.Y - spritevect.Y;
isDragging = true;//If you move the mouse too fast, it will sometimes move it out of the box before the box can move. This is my "Fix".
}
isDown = true;//The left mouse is now down, for later uses
}
else if (isDown && mouse.RightButton == ButtonState.Released)//If the right mouse button was previously pressed and now isn't
{
this.IsMouseVisible = true;//Make it visible
isDown = false;//No longer pressed
isDragging = false;//We're no longer dragging.
}
else if (isDown)
{
if (((mouse.X > spritevect.X && mouse.X < spritevect.X + spritetext.Width) && (mouse.Y > spritevect.Y && mouse.Y < spritevect.Y + spritetext.Height)) || isDragging)
{//Same concept as above, except now if we're dragging it'll still come in here
spritevect.X = mouse.X - xoffset;//Move it
spritevect.Y = mouse.Y - yoffset;
}
}
Thanks.
|
|
|
Jul 28, 2009, 05:52 PM // 17:52
|
#2
|
Ascalonian Squire
Join Date: Jul 2009
Profession: R/N
|
Since my last job had nothing to do with programming, i haven't kept up to date with XNA (i was using v2), however...
You could create a few variables to shorten the code. I think most of the mess comes from getting the sprite/mouse locations.
You have to remember that when coding applications like this, it's not always going to look pretty. I would offer to help out (you can PM me on the forums and we can mull over ideas), however 1- i haven't done this in a while, and 2 - i've never done anything complicated dealing with sprites. Most of my "game" type applications were written as a 2D top down (ala Final Fantasy 1) style.
|
|
|
Jul 29, 2009, 01:12 AM // 01:12
|
#3
|
Technician's Corner Moderator
Join Date: Jan 2006
Location: The TARDIS
Guild: http://www.lunarsoft.net/ http://forums.lunarsoft.net/
|
Change your comment placement to above the code you'll be referring to.
|
|
|
Jul 29, 2009, 01:21 AM // 01:21
|
#4
|
Ascalonian Squire
Join Date: Jul 2009
Profession: R/N
|
Tarun is correct. It's just a "nicety" (by the way, Tarun...nice Avatar!).
For example:
Code:
MouseState mouse = Mouse.GetState();//Mouse functions/vars
if (!isDown && mouse.RightButton == ButtonState.Pressed)//If the right mouse button was previously not pressed and now is...
{
Instead of your comment being right after the conditional statement, you should do this:
Code:
MouseState mouse = Mouse.GetState();//Mouse functions/vars
//If the right mouse button was previously not pressed and now is...
if (!isDown && mouse.RightButton == ButtonState.Pressed)
{
|
|
|
Thread Tools |
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 01:21 AM // 01:21.
|